﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



Imports System.Web
Imports System.Security.Principal

''' <summary>
''' Base business layer object
''' </summary>
''' <remarks>
''' This is an adaptation of the BizObject class described in
''' "ASP.NET 2.0 Website Programming (Problem - Design - Solution)" 
''' by Marco Bellinaso
''' Publisher: Wrox (www.wrox.com)
''' </remarks>
Public MustInherit Class BizObject

    Protected Const MAXROWS = Integer.MaxValue


#Region "Shared Methods"

    ''' <summary>
    ''' Used for DataGrid and DataView paging when using ObjectDataSource queries
    ''' </summary>
    ''' <param name="StartRowIndex">Start row in current page</param>
    ''' <param name="MaximumRows">Number of rows per page</param>
    ''' <returns>PageIndex (Integer)</returns>
    ''' <remarks>rss - 8/12/2006</remarks>
    Protected Shared Function GetPageIndex(ByVal StartRowIndex As Integer, ByVal MaximumRows As Integer) As Integer
        If MaximumRows <= 0 Then
            Return 0
        Else
            Return CType(Math.Floor(CType(StartRowIndex, Double) / CType(MaximumRows, Double)), Integer)
        End If
    End Function


    ''' <summary>
    ''' Quick function to remove invalid character ' from TSQL queries
    ''' </summary>
    ''' <param name="Value">Value to make replacement</param>
    ''' <returns>String</returns>
    ''' <remarks>rss - 8/12/2006</remarks>
    Protected Shared Function SqlEncode(ByVal Value As String) As String
        Return Value.Replace("'", "''")
    End Function


    ''' <summary>
    ''' Attempts to correctly format user entered text to like html format
    ''' </summary>
    Protected Shared Function EncodeText(ByVal Value As String) As String

        Return Value.Replace("  ", "&nbsp;&nbsp;").Replace(vbCrLf, "<br />")

    End Function

    ''' <summary>
    ''' Converts null database return values to an empty string or returns string value
    ''' </summary>
    ''' <param name="Value">Character type from database</param>
    ''' <returns>String</returns>
    ''' <remarks>rss - 8/12/2006</remarks>
    Protected Shared Function ConvertNullToEmptyString(ByVal Value As String) As String
        If String.IsNullOrEmpty(Value) Then
            Return ""
        Else
            Return Trim(Value.ToString)
        End If
    End Function



    ''' <summary>
    ''' Converts null database return values to 0 or returns Integer value
    ''' </summary>
    ''' <param name="Value">Numeric type from database</param>
    ''' <returns>Integer</returns>
    ''' <remarks>rss - 8/12/2006</remarks>
    Protected Shared Function ConvertNullToIntegerZero(ByVal Value As Object)
        If Value Is Nothing Then
            Return 0
        Else
            Return CType(Value, Integer)
        End If
    End Function


    ''' <summary>
    ''' Converts null database return value to 0 or returns Double value
    ''' </summary>
    ''' <param name="Value">Numeric type from database</param>
    ''' <returns>Double</returns>
    ''' <remarks>rss - 8/12/2006</remarks>
    Protected Shared Function ConvertNullToDoubleZero(ByVal Value As Object) As Double
        If Value Is Nothing Then
            Return 0
        Else
            Return CType(Value, Double)
        End If
    End Function


    ''' <summary>
    ''' Adds an object to the end of the ObjectArray passed
    ''' </summary>
    ''' <param name="ObjectArray">Array of objects passed by reference</param>
    ''' <param name="NewObject">Object to be added to the end of the ObjectArray passed</param>
    ''' <remarks>rss - 8/12/2006</remarks>
    Protected Sub AddObjectToArray(ByRef ObjectArray() As Object, ByVal NewObject As Object)

        ReDim Preserve ObjectArray(UBound(ObjectArray) + 1)
        ObjectArray(UBound(ObjectArray)) = NewObject

    End Sub

    ''' <summary>
    ''' PurgeCacheItems
    ''' </summary>
    ''' <param name="prefix">String</param>
    ''' <remarks>rss - 11/8/2006</remarks>
    Protected Shared Sub PurgeCacheItems(ByVal prefix As String)
        prefix = prefix.ToLower
        Dim itemsToRemove As New List(Of String)
        Dim enumerator As IDictionaryEnumerator = BizObject.Cache.GetEnumerator()
        While enumerator.MoveNext
            If enumerator.Key.ToString().ToLower().StartsWith(prefix) Then
                itemsToRemove.Add(enumerator.Key.ToString())
            End If
        End While
        Dim itemToRemove As String = ""
        For Each itemToRemove In itemsToRemove
            BizObject.Cache.Remove(itemToRemove)
        Next
    End Sub

#End Region


#Region "Properties"

    ''' <summary>
    ''' Quick reference to HttpContext.Current.Cache
    ''' </summary>
    ''' <returns>Caching.Cache</returns>
    ''' <remarks>rss - 8/12/2006</remarks>
    Protected Shared ReadOnly Property Cache() As Caching.Cache
        Get
            Return HttpContext.Current.Cache
        End Get
    End Property



    ''' <summary>
    ''' Quick reference to HttpContext.Current.User
    ''' </summary>
    ''' <returns>IPrincipal</returns>
    ''' <remarks>rss - 8/12/2006</remarks>
    Protected Shared ReadOnly Property CurrentUser() As IPrincipal
        Get
            Return HttpContext.Current.User
        End Get
    End Property



    ''' <summary>
    ''' Quick reference to HttpContext.Current.User.Identity.Name if user is authenticated
    ''' </summary>
    ''' <returns>String</returns>
    ''' <remarks>rss - 8/12/2006</remarks>
    Protected Shared ReadOnly Property CurrentUserName() As String
        Get
            Dim Username As String = ""
            If HttpContext.Current.User.Identity.IsAuthenticated Then
                Username = HttpContext.Current.User.Identity.Name
            End If
            Return Username
        End Get
    End Property



    ''' <summary>
    ''' Quick reference to HttpContext.Current.Request.UserHostAddress
    ''' </summary>
    ''' <returns>String</returns>
    ''' <remarks>rss - 8/12/2006</remarks>
    Protected Shared ReadOnly Property CurrentUserIP() As String
        Get
            Return HttpContext.Current.Request.UserHostAddress
        End Get
    End Property



    Protected Shared ReadOnly Property ServerName() As String
        Get
            Dim context As HttpContext = HttpContext.Current
            'Dim server As String = HttpContext.Current.Request.ServerVariables("SERVER_NAME")
            Dim server As String = context.Request.Url.Host.ToString
            Dim port As String = context.Request.ServerVariables("SERVER_PORT")
            If Not String.Equals(port, "80") Then server &= ":" & port
            Dim virtual_directory As String = context.Request.ApplicationPath
            If Not String.Equals(virtual_directory, "/") Then
                If Not server.EndsWith(virtual_directory) And Not server.EndsWith(virtual_directory & "/") Then server &= virtual_directory
            End If
            If Not server.EndsWith("/") Then server &= "/"

            Return server
        End Get
    End Property




    Private _created_date As DateTime
    Public Property created_date() As DateTime
        Get
            Return _created_date
        End Get
        Set(ByVal value As DateTime)
            _created_date = value
        End Set
    End Property

    Private _last_modified_date As DateTime
    Public Property last_modified_date() As DateTime
        Get
            Return _last_modified_date
        End Get
        Set(ByVal value As DateTime)
            _last_modified_date = value
        End Set
    End Property

    Private _last_modified_by As String
    Public Property last_modified_by() As String
        Get
            Return _last_modified_by
        End Get
        Set(ByVal value As String)
            _last_modified_by = value
        End Set
    End Property

#End Region




End Class
